<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_model extends CI_Model {

	function __construct() {
		parent::__construct();

		$this->load->library('LogSql');
	}

	/**
	 * 사용자 정보 가져오기]
	 * package_cd는 비회원이면 0, 회원이면 1로 세팅
	 */
	function get_member($user_id) {
		log_message('debug', "#### Login_model::get_member($user_id)");
		
		$default_prem_cd = $user_id == 'guests' ? PREM_GUEST : PREM_GENERAL;

		$sql  = PHP_EOL." SELECT  ";
		$sql .= PHP_EOL."       M.* ";
		$sql .= PHP_EOL."     , IFNULL( ( SELECT package_cd  ";
		$sql .= PHP_EOL."         FROM  tb_member_package  ";
		$sql .= PHP_EOL."         WHERE user_id = M.user_id  ";
		$sql .= PHP_EOL."         AND   DATE_FORMAT(CURDATE(),'%Y%m%d') BETWEEN start_dt AND end_dt ";
		$sql .= PHP_EOL."       ), '$default_prem_cd' ) package_cd ";
		$sql .= PHP_EOL." FROM  tb_member M ";
		$sql .= PHP_EOL." WHERE M.use_yn = 'Y'  ";
		$sql .= PHP_EOL." AND   M.user_id = ? ";
		$query = $this->db->query($sql, array($user_id)) or $this->logsql->log_sql('error');
		$result = $query->row_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}

	/**
	 * 최근 로그인 시간, IP 기록
	 */
	function update_login_info($user_id, $login_ip) {
		log_message('debug', "#### Login_model::update_login_info($user_id, $login_ip)");

		$this->db->set('login_ip', $login_ip);
		$this->db->set('login_dt', 'now()', FALSE);
		$this->db->where('user_id', $user_id);
		$this->db->update('tb_member') or $this->logsql->log_sql('error');
		
		$this->logsql->log_sql();

		return TRUE;
	}

	/**
	 * 아이핀 DI코드로 아이디 검색
	 */
	function get_member_id($ipin_di) {
		log_message('debug',"#### Login_model::get_member_id($ipin_di)");

		$this->db->select('user_id');
		$this->db->where('ipin_di', $ipin_di);
		$this->db->where('use_yn', 'Y');
		$query = $this->db->get('tb_member');
		$result = $query->row_array();

		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		if (count($result) > 0) {
			return $result['user_id'];
		}
        return '';
	}

	/**
	 * 아이핀 DI코드로 아이디/이메일 검색
	 */
	function get_member_id_email($ipin_di) {
		log_message('debug',"#### Login_model::get_member_id_email($ipin_di)");

		$this->db->select('user_id, email');
		$this->db->where('ipin_di', $ipin_di);
		$this->db->where('use_yn', 'Y');
		$query = $this->db->get('tb_member');
		$result = $query->row_array();

		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		if (count($result) > 0) {
			return $result;
		}
        return '';
	}
	
	/**
	 * 패스워드 변경
	 */
	function update_member_pwd($user_id, $pwd_crypt) {
		$this->db->set('pwd',$pwd_crypt);
		$this->db->where('user_id', $user_id);
		$this->db->where('use_yn', 'Y');
		$this->db->update('tb_member') or $this->logsql->log_sql('error');

		$this->logsql->log_sql();
	}
}

/* End of file login_model.php */
/* Location: ./application/models/auth/login_model.php */